package com.example.baselibs.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * activity管理类
 */
public class ActivityManager {
    private static ActivityManager activityManager;
    private static List<Activity> activities=new ArrayList<>();

    public static synchronized ActivityManager getInstance(){
        if (activityManager==null){
            activityManager=new ActivityManager();
        }
        return activityManager;
    }

    /**
     * 添加activity
     * @param activity
     */
    public void addActivity(Activity activity){
        activities.add(activity);
    }

    /**
     * 删除activity
     * @param activity
     */
    public void removeActivity(Activity activity){
        activities.remove(activity);
    }

    /**
     * 结束所有activity
     */
    public static void finishAllActivity(){
        for (int i=0;i<activities.size();i++){
            if (activities.get(i)!=null){
                activities.get(i).finish();
            }
        }
        activities.clear();
    }

    /**
     * 结束自己以外的activity
     */
    public static void finishExceptOwnActivity(Activity act){
        for(Activity avtivity : activities){
            if(act == avtivity)continue;
            avtivity.finish();
        }
    }
}
